﻿using System;
using System.Threading;
#if NETFX_CORE
using Windows.UI.Xaml;
#endif

namespace DaveSexton.Labs.Tests.Category1.Console.Foo
{
	public class WatchLab : BaseConsoleLab
	{
		protected override void Main()
		{
			int count = 25;

			TraceLine("Please wait for {0} ticks.", count);
			TraceLine();

			var interval = TimeSpan.FromSeconds(.5);

			using (var wait = new AutoResetEvent(false))
			{
#if NETFX_CORE
				var timer = new DispatcherTimer()
				{
					Interval = interval
				};

				timer.Tick += (_, __) =>
					{
#else
				using (var timer = new Timer(_ =>
					{
#endif
						TraceLine("Tick: {0}", count);

						count--;

						if (count == 0)
						{
#if NETFX_CORE
							timer.Stop();
							timer = null;
#endif

							wait.Set();
						}
#if NETFX_CORE
					};

				timer.Start();

				wait.WaitOne();
#else
					}, 
					null,
					TimeSpan.Zero,
					interval))
				{
					wait.WaitOne();
				}
#endif
			}

			TraceLine("Tock.");
		}
	}
}